package com.zzhou.linkinfo.clock.util.node;

import android.text.TextUtils;

import com.zzhou.linkinfo.clock.entity.Person;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Carlos on 2018-08-16.
 */

public class Node {

    public static final int Level_Top = 0;
    public static final int Level_School = 1;
    public static final int Level_Grade = 2;
    public static final int Level_Class = 3;
    public static final int Level_Member = 4;
    /**
     * 传入的实体对象
     */
    public Person bean;
    /**
     * 设置开启 关闭的图片
     */
    public int iconExpand = -1, iconNoExpand = -1;

    private long id;
    /**
     * 根节点pId为0
     */
    private long pId;

    private String name;

    /**
     * 当前的级别
     */
    private int level;

    /**
     * 是否展开
     */
    private boolean isExpand = false;

    private int icon = -1;

    /**
     * 下一级的子Node
     */
    private List<Node> children = new ArrayList<>();

    /**
     * 父Node
     */
    private Node parent;
    /**
     * 是否被checked选中
     */
    private boolean isChecked;
    /**
     * 是否为新添加的
     */
    public boolean isNewAdd = true;

    /**
     * 该分组下的人数
     */
    private int count;

    public boolean isChecked() {
        return isChecked;
    }

    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }

    public Node() {
    }

    public Node(long id, long pId, int level, Person bean) {
        super();
        this.id = id;
        this.pId = pId;
        this.level = level;
        this.bean = bean;
    }

    public Node(long id, long pId, String name, int count) {
        this.id = id;
        this.pId = pId;
        this.name = name;
        this.count = count;
    }

    public Person getBean() {
        return bean;
    }

    public void setBean(Person bean) {
        this.bean = bean;
    }

    public int getIcon() {
        return icon;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public long getpId() {
        return pId;
    }

    public void setpId(long pId) {
        this.pId = pId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public List<Node> getChildren() {
        return children;
    }

    public void setChildren(List<Node> children) {
        this.children = children;
    }

    public Node getParent() {
        return parent;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    /**
     * 是否为跟节点
     *
     * @return
     */
    public boolean isRoot() {
        return parent == null;
    }

    /**
     * 判断父节点是否展开
     *
     * @return
     */
    public boolean isParentExpand() {
        if (parent == null)
            return false;
        return parent.isExpand();
    }

    /**
     * 是否是叶子界点
     *
     * @return
     */
    public boolean isLeaf() {
        return children.size() == 0;
    }

    /**
     * 获取level
     */
    public int getLevel() {

        return parent == null ? 0 : parent.getLevel() + 1;
    }

    public int getMyLevel() {
        return level;
    }

    /**
     * 设置展开
     *
     * @param isExpand
     */
    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        if (!isExpand) {
            for (Node node : children) {
                node.setExpand(isExpand);
            }
        }
    }

    @Override
    public String toString() {
        return "{\"id\":" + id +
                ", \"pId\":" + pId +
                ", \"name\":\"" + name + '\"' +
                ", \"level\":" + level +
                ", \"isChecked\":" + isChecked +
                ", \"isExpand\":" + isExpand +
                "}";
    }

    public static String toJson(List<Node> nodes) {
        StringBuffer buffer = new StringBuffer();
        if (nodes != null) {
            if (nodes.size() == 1) {
                buffer.append("[");
                buffer.append(nodes.get(0).toString());
                buffer.append("]");
            } else {
                for (int i = 0; i < nodes.size(); i++) {
                    if (i == 0) {
                        buffer.append("[");
                        buffer.append(nodes.get(i).toString());
                    } else if (i == nodes.size() - 1) {
                        buffer.append(",");
                        buffer.append(nodes.get(i).toString());
                        buffer.append("]");
                    } else {
                        buffer.append(",");
                        buffer.append(nodes.get(i).toString());
                    }
                }
            }
        }
        return buffer.toString();
    }

    public static List<Node> parseJson(String json) {
        List<Node> nodes = new ArrayList<>();
        if (!TextUtils.isEmpty(json)) {
            JSONArray jsonArray = null;
            try {
                jsonArray = new JSONArray(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            if (jsonArray != null && jsonArray.length() > 0) {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.optJSONObject(i);
                    Node node = new Node();
                    node.setId(jsonObject.optLong("id"));
                    node.setpId(jsonObject.optLong("pId"));
                    node.setName(jsonObject.optString("name"));
                    node.setLevel(jsonObject.optInt("level"));
                    node.setChecked(jsonObject.optBoolean("isChecked"));
                    node.setExpand(jsonObject.optBoolean("isExpand"));
                    nodes.add(node);
                }

            }
        }
        return nodes;
    }
}
